package com.leosoft.heatmonitor.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.leosoft.heatmonitor.model.Meter;

@Component
public class MeterDAO extends BaseDAO {
	public Map<String, Object> list(int startIndex, int length, List<String[]> wherePart, List<String[]> orderPart,
			String userId) {
		String midSql = "SELECT m.id, m.deviceKey, m.STATUS, m.allflow, m.allheat,m.lastheat, m.currentflow, m.inTemperature, m.outTemperature, m.currentHeat, m.addr1,m.name FROM meter m";
		String totalSql = null;
		if (!userId.isEmpty()) {
			midSql += ", device d,project p,control c where m.deviceKey = d.key and d.project_id = p.id and c.project_id = p.id and c.user_id = '"
					+ userId + "' ";
			totalSql = "select count(m.id) from (" + midSql + ") a";
		}else{
			totalSql = "select count(id) from meter";
		}
		Object total = this.getSession().createSQLQuery(totalSql).uniqueResult();
		String sql = "select id, deviceKey, STATUS, allflow, allheat,lastheat, currentflow, inTemperature, outTemperature,  currentHeat, addr1, name from ("
				+ midSql + ") a";
		if (!wherePart.isEmpty()) {
			sql += " where ";
			for (String[] where : wherePart) {
				sql += where[0] + " like '%" + where[1] + "%' and ";
			}
			sql = sql.substring(0, sql.length() - 4);
		}
		String filterSql = "select count(id) from (" + sql + ") b";
		Object recordsFiltered = this.getSession().createSQLQuery(filterSql).uniqueResult();
		if (!orderPart.isEmpty()) {
			sql += " order by ";
			for (String[] order : orderPart) {
				sql += order[0] + " " + order[1] + ",";
			}
			sql = sql.substring(0, sql.lastIndexOf(","));
		}
		sql += " limit " + startIndex + "," + length;
		List<Object[]> meters = this.getSession().createSQLQuery(sql).list();
		List<Map<String, Object>> resultList = new ArrayList<>();
		for (Object[] meter : meters) {
			Map<String, Object> result = new HashMap<>();
			result.put("id", meter[0]);
			result.put("deviceKey", meter[1]);
			result.put("status", meter[2]);
			result.put("allflow", meter[3]);
			result.put("allheat", meter[4]);
			result.put("lastheat", meter[5]);
			result.put("currentflow", meter[6]);
			result.put("inTemperature", meter[7]);
			result.put("outTemperature", meter[8]);
			result.put("currentHeat", meter[9]);
			result.put("addr1", meter[10]);
			result.put("name", meter[11]);
			resultList.add(result);
		}

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("recordsTotal", total);
		result.put("recordsFiltered", recordsFiltered);
		result.put("data", resultList);
		return result;
	}

	public boolean notExist(String deviceKey, String addr1, String name) {
		Criteria c = this.getSession().createCriteria(Meter.class);
		c = c.add(Restrictions.eq("name", deviceKey));
		c = c.add(Restrictions.eq("deviceKey", deviceKey));
		c = c.add(Restrictions.eq("addr1", addr1));

		return c.list().isEmpty();
	}

	public void create(String deviceKey, String addr1, String name) {
		Meter meter = new Meter();
		meter.setName(name);
		meter.setDeviceKey(deviceKey);
		meter.setAddr1(addr1);
		this.getSession().merge(meter);

	}

	public Meter load(String id) {
		return (Meter) this.getSession().load(Meter.class, id);
	}

	public void update(String id, String deviceKey, String addr1, String name) {
		Meter meter = load(id);
		meter.setName(name);
		meter.setDeviceKey(deviceKey);
		meter.setAddr1(addr1);
		this.getSession().merge(meter);
	}

	public void delete(String id) {
		Meter meter = load(id);
		this.getSession().delete(meter);
	}
}
